package com.frx.sort.countsprt;

import java.util.Arrays;

/**
 * @author ：frx
 * @date ：Created in 2019/10/29 19:09
 * @description：ToDO
 * @modified By：
 * @version: ToDO
 */
public class TestTwo {

    public static void main(String[] args){
        int[] arr = {2,3,5,3,3,6,5,2};
        System.out.println(Arrays.toString(arr));
        sortCount(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void sortCount(int[] arr){
        int max = 0;
        for (int i=0;i<arr.length;i++){
            if (arr[i] > max){
                max = arr[i];
            }
        }
        int[] indexArr = new int[max+1];
        for (int i=0;i<arr.length;i++){
            indexArr[arr[i]]++;
        }
        System.out.println(Arrays.toString(indexArr));
        for (int i=1;i<indexArr.length;i++){
            indexArr[i] = indexArr[i] + indexArr[i-1];
        }
        System.out.println(Arrays.toString(indexArr));
        int[] newArr = new int[arr.length];
        for (int i=0;i<arr.length;i++){
            int index = arr[i];
            int count = indexArr[index];
            newArr[--count] = index;
            indexArr[index]--;
        }
        for (int i=0;i<arr.length;i++){
            arr[i] = newArr[i];
        }
    }

}
